package com.douya.common.pojo.dto;


import com.douya.common.utils.LocalDataUtils;
import com.douya.common.utils.DateUtils;
import lombok.*;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;

/**
 * 说明：查询条件对象
 *
 * @Author 胡晨烨
 * @Date 2020/02/01
 */
@Getter
@Setter
@NoArgsConstructor
@Accessors(chain = true)
public class ConditionDTO {

    private String name;

    private String compare;
    private String pattern;
    private String value;
    private String type;

    public CompareType realCompare() {
        if (CompareType.EQ.value.equals(this.compare)) {
            return CompareType.EQ;
        }
        if (CompareType.NE.value.equals(this.compare)) {
            return CompareType.NE;
        }
        if (CompareType.LIKE.value.equals(this.compare)) {
            return CompareType.LIKE;
        }
        if (CompareType.GT.value.equals(this.compare)) {
            return CompareType.GT;
        }
        if (CompareType.GE.value.equals(this.compare)) {
            return CompareType.GE;
        }
        if (CompareType.LT.value.equals(this.compare)) {
            return CompareType.LT;
        }
        if (CompareType.LE.value.equals(this.compare)) {
            return CompareType.LE;
        }
        if (CompareType.IN.value.equals(this.compare)) {
            return CompareType.IN;
        }
        if (CompareType.NOT_IN.value.equals(this.compare)) {
            return CompareType.NOT_IN;
        }
        if (CompareType.NULL.value.equals(this.compare)) {
            return CompareType.NULL;
        }
        if (CompareType.NOT_NULL.value.equals(this.compare)) {
            return CompareType.NOT_NULL;
        }
        if (CompareType.NOT_LIKE.value.equals(this.compare)) {
            return CompareType.NOT_LIKE;
        }

        return null;
    }

    @SuppressWarnings("rawtypes")
    public Comparable realValue() {
        if (ClassType.BYTE.value.equals(this.type)) {
            return Byte.valueOf(this.value);
        }
        if (ClassType.INTEGER.value.equals(this.type)) {
            return Integer.valueOf(this.value);
        }
        if (ClassType.LONG.value.equals(this.type)) {
            return Long.valueOf(this.value);
        }
        if (ClassType.DATE.value.equals(this.type)) {
            if (StringUtils.isNotBlank(this.pattern)) {
                return DateUtils.parseDate(this.value, this.pattern);
            } else {
                return DateUtils.parseDate(this.value, "yyyy-MM-dd", "yyyy/MM/dd", "yyyyMMdd",
                        "yyyy-MM-dd HH", "yyyy/MM/dd HH", "yyyyMMddHH",
                        "yyyy-MM-dd HH:mm", "yyyy/MM/dd HH:mm", "yyyyMMddHHmm",
                        "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss", "yyyyMMddHHmmss");
            }
        }
        if (ClassType.LOCAL_DATE.value.equals(this.type)) {
            return StringUtils.isNotBlank(this.pattern) ? LocalDataUtils.parseDate(this.value, this.pattern) :
                    LocalDataUtils.parseDateWithDefaultPatterns(this.value);
        }
        if (ClassType.LOCAL_DATE_TIME.value.equals(this.type)) {
            return StringUtils.isNotBlank(this.pattern) ? LocalDataUtils.parseDateTime(this.value, this.pattern) :
                    LocalDataUtils.parseDateTimeWithDefaultPatterns(this.value);
        }
        if (ClassType.STRING.value.equals(this.type)) {
            return this.value;
        }
        if (ClassType.BOOLEAN.value.equals(this.type)) {
            return Boolean.valueOf(this.value);
        }
        if (ClassType.BIG_DECIMAL.value.equals(this.type)) {
            return BigDecimal.valueOf(Double.parseDouble(this.value));
        }
        return null;
    }

    public enum CompareType {
        /**
         * 相等, 不等, 小于, 小于等于, 大于, 大于等于, like,
         */
        EQ("eq"), NE("ne"), LT("lt"), LE("le"),
        GT("gt"), GE("ge"), LIKE("like"), IN("in"),
        NULL("nl"), NOT_NULL("nnl"), NOT_LIKE("nl"), NOT_IN("nin");

        final String value;

        CompareType(String value) {
            this.value = value;
        }
    }

    public enum ClassType {
        /**
         * 参数类型：byte, int, long, Date, string, boolean, localDate, localDatetime ,bigdecimal
         */
        BYTE("byte"), INTEGER("int"), LONG("long"), DATE("date"), STRING("str"),
        BOOLEAN("bol"), LOCAL_DATE("ld"), LOCAL_DATE_TIME("ldt"), BIG_DECIMAL("bd");

        final String value;

        ClassType(String value) {
            this.value = value;
        }
    }
}
